#############################################################################################################
# fig3: Adjusted and conventional macropartisanship indices for the US, the UK, Japan, Germany, and Denmark #
#############################################################################################################


library(ggplot2)
library(tidyverse)
library(zoo)
library(gridExtra)
library(psych)
library(cowplot)
library(lubridate)





###-----usa

#load data
usa<-read.csv("USA_FINAL.csv")
attach(usa)

#calculating classic macroaprtisanship
usa$perdemocrat<-democrat/(democrat+republican)

#1st step:basic fomulation for the new index
usa$macropartisanship_1<-(democrat/(democrat+republican))*(enpp_np/2)*(((democrat+republican)/independent)*(1/enpp_np^2))
describe(usa$macropartisanship_1)

#2nd step: calculating variances
usa$macrop_var<-var(usa$macropartisanship_1)
usa$macroper_var<-var(usa$perdemocrat)

#3rd step: weighting process
usa$macropartisanship_USA<-(usa$macropartisanship_1/usa$macrop_var+usa$perdemocrat/usa$macroper_var)/(1/usa$macrop_var+1/usa$macroper_var)


#plot the classic macropartisanship and the newly-defined one
df<-data.frame(usa$yearMon,usa$perdemocrat,usa$macropartisanship_USA) 
names(df)[2]<-"Major party share(conventional macropartisanship)"
names(df)[3]<-"Newly defined Macropartisanship"
df2<-df %>% 
  gather(key="Indices", value="value", -usa.yearMon)

#plot the us case (same as Fig2)
usa_fig<-ggplot(df2, aes(x = as.yearmon(as.Date(df2$usa.yearMon)), y = value)) + 
  geom_line(aes(color = Indices), size = 1) +
  scale_color_manual(values = c("grey", "black")) +
  theme_minimal()+
  xlab("Year")+
  theme(legend.position = 'none')+
  ggtitle("USA (Mean ENPP:1.94)")


###-------uk
df_mp_uk<-read.csv("UK_FINAL.csv")
attach(df_mp_uk)
summary(df_mp_uk)
df_mp_uk$percon<-df_mp_uk$dfmean_con/df_mp_uk$allp_uk

#1st step:basic fomulation for the new index

df_mp_uk$macropartisanship1<-(dfmean_con.mean.Con/df_mp_uk$allp_uk)*(enpp_np/2)*((allp_uk/perindependent)*(1/enpp_np^2))
describe(df_mp_uk$macropartisanship1)

#2nd step: calculating variances
df_mp_uk$macropartisanship1<-ifelse((is.infinite(df_mp_uk$macropartisanship1) & (df_mp_uk$macropartisanship1> 0)) , 0.01, df_mp_uk$macropartisanship1)
macrop_var<-var(df_mp_uk$macropartisanship1)
macroper_var<-var(df_mp_uk$percon)

#3rd step: weighting process
df_mp_uk$macropartisanship_UK<-(df_mp_uk$macropartisanship1/macrop_var+df_mp_uk$percon/macroper_var)/
  (1/macrop_var+1/macroper_var)


#plot the modified macropartisanship
macroAns<-data.frame(df_mp_uk$yearMon,df_mp_uk$time_id,df_mp_uk$macropartisanship_UK)


#plot the classic macropartisanship and the newly-defined one
df<-data.frame(df_mp_uk$time_id,df_mp_uk$macropartisanship_UK,df_mp_uk$percon) 
df2<-df %>% 
  select(df_mp_uk.time_id,df_mp_uk.macropartisanship_UK,df_mp_uk.percon) %>%
  gather(key="variable", value="value", -df_mp_uk.time_id)

uk_fig<-ggplot(df2, aes(x = df_mp_uk.time_id, y = value)) + 
  geom_line(aes(color = variable), size = 1) +
  scale_color_manual(values = c("black", "grey")) +
  theme_minimal()+
  xlab("Year")+
  theme(legend.position = 'none')+
  ggtitle("UK (Mean ENPP:2.22)")


###--------japan
#load data
b<-read.csv("JAPAN_FINAL.csv")
attach(b)

#percentage of ldp support
b$ldp_share<-ldp/allp
indep_jp<-100-allp

###efficient party numbers: with vote share
#squared values of each party share
ob<-cbind(jldp_v,jjsp_v,jjcp_v,jother_v,jindep_v,jminkai_v,jminnna_v,jminsha_v,jminshu_v,
          jkomei_v,jkokuminshin_v,jkokoro_v,jnewlib_v,jsdf_v,jshinpo_v,jshinsei_v,jshinshin_v,jshintonihon_v,
          jsakigake_v,jjiyu_v,jjiyuren_v,jjisedai_v,jhoshu_v,jhoshushin_v,jkaikaku_v,jseikatu_j,
          jrikken_v,jkibou_v)


#1st step:basic fomulation for the new index
b$macropartisanship1<-(ldp/100)*(enpp_np/2)*((allp/indep_jp)*(1/enpp_np^2))
describe(b$macropartisanship1)


#2nd step: calculating variances
b$macrop_var<-var(b$macropartisanship1,na.rm=T)
b$macroper_var<-var(b$ldp_share,na.rm=T)

#3rd step: weighting process
b$macropartisanship_jp<-(b$macropartisanship1/b$macrop_var+b$ldp_share/b$macroper_var)/(1/b$macrop_var+1/b$macroper_var)

#plot the classic macropartisanship and the newly-defined one
df<-data.frame(b$yearMon,b$ldp_share,b$macropartisanship_jp) 
df2<-df %>% 
  gather(key="variable", value="value", -b.yearMon)

japan_fig<-ggplot(df2, aes(x = as.yearmon(b.yearMon), y = value)) + 
  geom_line(aes(color = variable), size = 1) +
  scale_color_manual(values = c("black", "grey")) +
  theme_minimal()+
  xlab("Year")+
  theme(legend.position = 'none')+
  ggtitle("Japan (Mean ENPP:2.69)")



###-------german
df_meangr<-read.csv("GERMAN_FINAL.csv")
attach(df_meangr)

#calculating the sum of percentage and the percentage of the independent
allp_gr=.n+ADM_ALL+AFD_AL+ALFA+andere.P+BP.Bay+BundFre+Bundnis+Burgerpa+BuSo.Bu+
  CDU+CM_Chri+DA+DasNeue+DBD+DieViol+DKP+DSU+DVU+Familie+Feminist+FWD_Fr+
  GAZ+GLU+Graue.Pa+Grune+MLPD_M+NPD+ODP+ODPD+PBC_Pa+PDS.die+Piratenp+PSG_Pa+
  Rentnerp+Republik+RRP...Re+Schill.P+SPD+Statt.Pa+Tierschu+WASG_w+Zentrum
indep_gr=1-allp_gr

#1st step:basic fomulation for the new index

df_meangr$macropartisanship1<-(CDU/allp_gr)*(enpp_np/2)*((allp_gr/indep_gr)*(1/enpp_np^2))
describe(df_meangr$macropartisanship1)
df_meangr$perCDU<-CDU/allp_gr

#2nd step: calculating variances
df_meangr$macrop_var<-var(df_meangr$macropartisanship1)
df_meangr$macroper_var<-var(df_meangr$perCDU)

#3rd step: weighting process
df_meangr$macropartisanship_GR<-(df_meangr$macropartisanship1/df_meangr$macrop_var+df_meangr$perCDU/df_meangr$macroper_var)/
  (1/df_meangr$macrop_var+1/df_meangr$macroper_var)



#plot the classic macropartisanship and the newly-defined one
df<-data.frame(df_meangr$yearMon,df_meangr$perCDU,df_meangr$macropartisanship_GR) 
df2<-df %>% 
  gather(key="variable", value="value", -df_meangr.yearMon)

german_fig<-ggplot(df2, aes(x = as.yearmon(df_meangr.yearMon), y = value)) + 
  geom_line(aes(color = variable), size = 1) +
  scale_color_manual(values = c("black", "grey")) +
  theme_minimal()+
  theme(legend.position = 'none')+
  xlab("Year")+
  ggtitle("Germany (Mean ENPP:3.71)")



###-------denmark
denmark_final<-read.csv("DENMARK_FINAL.csv")
attach(denmark_final)
denmark_final$indep_dnk=1-(denmark_final$allp_dnk/100)
denmark_final$socd<-mean.socd/100

#1st step:basic fomulation for the new index

denmark_final$macropartisanship_1<-(denmark_final$socd/(denmark_final$allp_dnk/100))*(enpp_np/2)*(((denmark_final$allp_dnk/100)/denmark_final$indep_dnk)*(1/enpp_np^2))
describe(denmark_final$macropartisanship_1)
denmark_final$perSOCD<-denmark_final$socd/(denmark_final$allp_dnk/100)

#2nd step: calculating variances
denmark_final$macropartisanship_1<-ifelse((is.infinite(denmark_final$macropartisanship_1) & (denmark_final$macropartisanship_1> 0)) , 0.01, denmark_final$macropartisanship_1)
denmark_final$macrop_var<-var(denmark_final$macropartisanship_1,na.rm=T)
denmark_final$macroper_var<-var(denmark_final$socd,na.rm=T)

#3rd step: weighting process
denmark_final$macropartisanship_DNK<-(denmark_final$macropartisanship_1/denmark_final$macrop_var+denmark_final$perSOCD/denmark_final$macroper_var)/
  (1/denmark_final$macrop_var+1/denmark_final$macroper_var)



#plot the classic macropartisanship and the newly-defined one
df<-data.frame(denmark_final$yearMon,denmark_final$socd,denmark_final$macropartisanship_DNK) 
df2<-df %>% 
  gather(key="variable", value="value", -denmark_final.yearMon)

denmark_fig<-ggplot(df2, aes(x = as.yearmon(denmark_final.yearMon), y = value)) + 
  geom_line(aes(color = variable), size = 1) +
  scale_color_manual(values = c("black", "grey")) +
  theme_minimal()+
  xlab("Year")+
  theme(legend.position = 'none')+
  ggtitle("Denmark (Mean ENPP:4.80)")




###unified five graphs and save

windows(20,15)
legend_b <- get_legend(usa_fig + theme(legend.position="bottom"))
fig1_b <- plot_grid( usa_fig,
                     uk_fig,
                     japan_fig,
                     german_fig,
                     denmark_fig,
                     align = 'vh',
                     hjust = -1,
                     nrow = 3
)

fig1 <- plot_grid( fig1_b, legend_b, ncol = 1, rel_heights = c(1, .2))
ggsave(file = "fig3_enppnp.pdf", plot = fig1)



